package com.ulook.lookplanet.ui.square.topic;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.Toast;

import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.pullLayout.QMUIPullLayout;
import com.ulook.lookplanet.R;
import com.ulook.lookplanet.base.BaseRecyclerAdapter;
import com.ulook.lookplanet.base.BaseViewHolder;
import com.ulook.lookplanet.common.LookPlanetApplication;
import com.ulook.lookplanet.data.model.BaseTopic;
import com.ulook.lookplanet.data.model.TCommunity;
import com.ulook.lookplanet.data.model.TTopic;
import com.ulook.lookplanet.main.MainActivity;
import com.ulook.lookplanet.ui.square.VArticlePreInfo;
import com.ulook.lookplanet.ui.square.article.ArticleActivity;
import com.ulook.lookplanet.widget.ArticleView;

import java.io.Serializable;

public class TopicActivity extends AppCompatActivity {

    private TopicViewModel viewModel;
    private QMUIPullLayout pullLayout;
    private RecyclerView recyclerView;
    private BaseRecyclerAdapter<VArticlePreInfo> mAdapter;
    private boolean isRefresh;
    private int currUserId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_topic);
        viewModel = new ViewModelProvider(this).get(TopicViewModel.class);

        Intent intent = getIntent();
        currUserId = intent.getIntExtra("currUserId", 0);
        BaseTopic obj = (BaseTopic)(intent.getSerializableExtra("topic"));
        viewModel.setBaseTopic(obj);

        viewModel.getArtInfos().observe(this, articlePreInfos -> {
            if(isRefresh){
                mAdapter.setData(articlePreInfos);
                recyclerView.scrollToPosition(0);
            }else {
                mAdapter.append(articlePreInfos);
            }
        });

        initTopBar();
        initData();
    }

    /**
     * 初始化TopBar
     */
    private void initTopBar(){
        QMUITopBarLayout topBar = findViewById(R.id.topic_bar);
        topBar.addLeftBackImageButton().setOnClickListener(view -> {
            finish();
        });
        String title = viewModel.getBaseTopic().getName();
        if(title.length() > 7){
            title = title.substring(0, 7)+"...";
        }
        topBar.setTitle(title);
    }

    /**
     * 初始化Data
     */
    private void initData(){
        pullLayout = findViewById(R.id.topic_pull_layout);
        recyclerView = findViewById(R.id.topic_recyclerView);

        // 监听 上拉/下拉 动作
        pullLayout.setActionListener(pullAction -> pullLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (pullAction.getPullEdge() == QMUIPullLayout.PULL_EDGE_TOP) {
                    isRefresh = true;
                    viewModel.getArticleData(currUserId, 0);
                } else if (pullAction.getPullEdge() == QMUIPullLayout.PULL_EDGE_BOTTOM) {
                    isRefresh = false;
                    viewModel.getArticleData(currUserId, mAdapter.getItemCount());
                }
                pullLayout.finishActionRun(pullAction);
            }
        }, 3000));
        // 设置布局
        recyclerView.setLayoutManager(new LinearLayoutManager(TopicActivity.this) {
            @Override
            public RecyclerView.LayoutParams generateDefaultLayoutParams() {
                return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            }
        });
        // 初始化Adapter
        mAdapter = new BaseRecyclerAdapter<VArticlePreInfo>(TopicActivity.this, null) {
            @Override
            public int getItemLayoutId(int viewType) {
                return R.layout.recycler_article;
            }
            @Override
            public void bindData(BaseViewHolder holder, int position, VArticlePreInfo item) {
                ArticleView articleView = holder.getItemView(R.id.recycler_article_view);
                articleView.setWidgetData(item, currUserId);
            }
        };
        // 添加item点击动作
        /*mAdapter.setOnItemClickListener((itemView, pos) -> {
            VArticlePreInfo articlePreInfo = mAdapter.getItem(pos);
            Intent intent = new Intent(this, ArticleActivity.class);
            intent.putExtra("currUserId", currUserId);
            intent.putExtra("article", articlePreInfo);
            startActivity(intent);
        });*/
        recyclerView.setAdapter(mAdapter);
        viewModel.getArticleData(currUserId, 0);
    }
}